/**
 * 
 */
package inf.mediasManagement;

import inf.dvdsManagement.Dvd;
import inf.exceptions.*;
import inf.hibernateUtils.HibernateUtil;
import inf.mediasManagement.Media;
import inf.specimensManagement.Specimen;
import inf.subscribersManagement.Subscriber;

import java.util.List;


import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;


/** 
 * @author Fabre, Malric
 */
public class MediaDAO {

	public static final String ISBN_ISAN = "isbnOrIsan";
	public static final String TITLE = "title";

	/**
	 * Adds a media in the persistence unit (hence in DB)
	 * 
	 * @param media
	 * @return boolean
	 */
	public static boolean save(Media media){
		return MediaDAO.add(media);
	}

	public static boolean add(Media media){
		Session session = HibernateUtil.openSession();
		try{
			for(Specimen spec : media.getSpecimens()){
				session.saveOrUpdate(spec);
			}
			session.saveOrUpdate(media);
		} catch (Exception e) {
			System.err.println("Problem when saving");
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * Removes an instance of a media.
	 * 
	 * @param media
	 * @return boolean
	 * @throws DvdExistsException
	 */
	public static boolean remove(Media media){
		Session session = HibernateUtil.openSession();
		try{
			for(Specimen spec : media.getSpecimens()){
				session.delete(spec);
			}
			session.delete(media);
		} catch (Exception e) {
			System.err.println("Problem when saving");
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public static boolean addSpecimen(Specimen specimen) {
		Session session = HibernateUtil.openSession();
		try{
			session.saveOrUpdate(specimen);		
			session.saveOrUpdate(specimen.getMedia());
		} catch(Exception e) {
			System.err.println("Problem when saving");
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public static boolean removeSpecimen(Specimen specimen){
		Session session = HibernateUtil.openSession();
		try{
			Media media = specimen.getMedia();
			session.delete(specimen);
			session.saveOrUpdate(media);
		} catch (Exception e) {
			System.err.println("Problem when saving");
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	

	/**
	 * Tests the presence of any media in DB
	 * 
	 * @return boolean
	 */
	@SuppressWarnings("unchecked")
	public boolean isEmpty(){
		Session session = HibernateUtil.openSession();
		Query query = session.createQuery("from medias");
		List<Media> medias = query.list();
		if (medias.isEmpty())
			return true;
		else
			return false;	
	}

	/**
	 * Gives the content of table medias in DB
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Media> getContent() {
		Session session = HibernateUtil.openSession();
		Query query = session.createQuery("from medias");
		List<Media> media = query.list();
		return media;
	}
	
	public static Specimen getSpecimen(long id) throws MediaExistsException {
		Session session = HibernateUtil.openSession();
		Specimen spec = (Specimen) session.get(Specimen.class, id);
		if (spec == null) throw new MediaExistsException();
		else return spec;
	}
	
	public static Media get(long id) throws MediaExistsException {
		Session session = HibernateUtil.openSession();
		Media m = (Media) session.get(Media.class, id);
		if (m == null) throw new MediaExistsException();
		else return m;
	}

	public static boolean contains(Media media) throws MediaExistsException {
		Session session = HibernateUtil.openSession();
		if (media == null) throw new MediaExistsException();
		Media m = (Media) session.get(Media.class, media.getId());
		if (m == null) return false;
		else return m.equals(media);	
	}

	/**
	 * Gets an instance of a media by ID classification
	 * 
	 * @param mediaId
	 * @return media
	 * @throws MediaExistsException 
	 * @throws DvdExistsException
	 */
	public static Media getById(int mediaId) throws MediaExistsException {
		Session session = HibernateUtil.openSession();
		Criteria criteria = session.createCriteria(Media.class).add(Restrictions.eq("id", mediaId));
		Media media = (Media)criteria.uniqueResult();
		if (media == null) throw new MediaExistsException();
		else
			return media;
	}

	/**
	 * Gets an instance of a media by IsbnOrIsan classification
	 * 
	 * @param Object isbnOrIsan
	 * @return List
	 * @throws DvdExistsException
	 */
	public static List<Media> getByIsbnOrIsan(Object isbnOrIsan) throws DvdExistsException {
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();
		Criteria criteria = session.createCriteria(Media.class).add(Restrictions.eq(ISBN_ISAN, isbnOrIsan));
		List<Media> media = (List<Media>)criteria.list();
		t.commit();

		return media;
	}

	/**
	 * Gets an instance of a media by property classification
	 * 
	 * @param Object title
	 * @return List
	 * @throws DvdExistsException
	 */
	public static List<Media> getByTitle(Object title) throws DvdExistsException {
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();
		Criteria criteria = session.createCriteria(Media.class).add(Restrictions.eq(TITLE, title));
		List<Media> media = (List<Media>)criteria.list();
		t.commit();	

		return media;
	}

	/**
	 * Number of medias in DB.
	 * 
	 * @return 
	 * @throws DvdExistsException
	 */
	public long size(){
		Session session = HibernateUtil.openSession();
		Query query = session.createQuery("from medias");
		List<Media> medias = query.list();
		return medias.size();
	}
}


